package com.hkts.flow.core.flowable.listener.global;

import com.dragon.tools.common.JsonUtils;
import com.hkts.flow.api.flowableTo.HkAssessmentScoreTo;
import com.hkts.flow.core.service.flowable.IFlowableTaskService;
import com.hkts.flow.feign.FeignInterface;
import com.hkts.flow.feign.FeignPerformance;
import com.hkts.flow.interceptor.FlowAbleInterceptor;
import com.hkts.flow.manager.utils.Result;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.engine.*;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassNameGlobleProcessCompleteListener
 * @Description 流程完成监听器 用于流程审核完成后数据状态回写
 * @Author yck
 * @Date2020-12-10 8:52
 * @Version V1.0
 **/
@Component
public class GlobleProcessCompleteListener extends AbstractFlowableEngineEventListener {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected RuntimeService runtimeService;
    @Autowired
    protected HistoryService historyService;
    @Autowired
    protected RepositoryService repositoryService;
    @Autowired
    protected TaskService taskService;
    @Autowired
    protected IdentityService identityService;

    @Autowired
    RestTemplate restTemplate;
    @Value("${performance.address}")
    private String address;

    // private static ApplicationContext applicationContext;

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {

            //得到流程定义id
            String processDefinitionId = event.getProcessDefinitionId();
            //得到流程实例id
            String processInstanceId = event.getProcessInstanceId();
            //1、授权
            //2、相邻节点自动跳过
            // restTemplate.
            //3、发送消息

        String restUrl = address+"/performance/hkAssessmentScore/updateStateByProcessInstanceId";
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("processInstanceId",processInstanceId);
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        ThreadLocal<Map<String, Object>> toThreadLocal = FlowAbleInterceptor.toThreadLocal;
        Map<String, Object> stringObjectMap = toThreadLocal.get();
        headers.set("accessToken",stringObjectMap.get("accessToken").toString());
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<Result> resultResponseEntity = client.postForEntity(restUrl, requestEntity, Result.class, method);
        if(resultResponseEntity.getBody().getCode().equals("200")){
            logger.error("流程数据回调失败processInstanceId:"+processInstanceId);
        }

    }
}
